<template>
    <el-dialog v-dialogDrag :title="dialogType==1 ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible"
        @close="handleClose" :center="true">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">
            <el-form-item label="菜单名称" prop="name">
                <el-input v-model="dataForm.name" placeholder="菜单名称或按钮名称"></el-input>
            </el-form-item>
            <el-form-item label="菜单URL" prop="url">
                <el-input v-model="dataForm.url" placeholder="菜单URL"></el-input>
            </el-form-item>

            <el-form-item label="菜单标识" prop="logo">
                <el-input v-model="dataForm.logo" placeholder="如: user_list"></el-input>
            </el-form-item>
            <el-form-item label="上级菜单" prop="parentid">
                <el-popover ref="menuListPopover" placement="bottom-start" trigger="click" v-model="visible2">
                    <div style="overflow-y:auto; width:200px; height:200px;">
                        <el-tree :data="menuList" :props="menuListTreeProps" node-key="id" ref="menuListTree"
                            @current-change="menuListTreeCurrentChangeHandle" :default-expand-all="true"
                            :highlight-current="true" :expand-on-click-node="false"></el-tree>
                    </div>
                </el-popover>
                <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true"
                    placeholder="点击选择上级菜单"></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="dataForm.type" placeholder="请选择" style="width:100%">
                    <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="dataForm.status">
                    <el-radio :label="1">启用</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="排序号" prop="sort">
                <el-input-number v-model="dataForm.sort" controls-position="right" :min="1" label="排序号">
                </el-input-number>
            </el-form-item>

            <el-form-item label="菜单图标" prop="icon">
                <upload :img="dataForm.icon" @update="update" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>
<script>
    import API from '@/api';
    import upload from '../../components/upLoad/index'
    export default {
        components: {
            upload
        },
        data() {
            var validateUrl = (rule, value, callback) => {
                if (this.dataForm.dialogType === 1 && !/\S/.test(value)) {
                    callback(new Error('菜单URL不能为空'));
                } else {
                    callback();
                }
            };
            return {
                typeOptions: [{
                        value: '1',
                        label: '菜单资源'
                    },
                    {
                        value: '2',
                        label: '页面资源'
                    }
                ],

                menuList: [],
                visible: false,
                visible2: false,
                dataForm: {
                    appId: '',
                    type: '1',
                    icon: '', // 菜单图标
                    url: '',
                    name: '',
                    parentid: 0,
                    status: 1,
                    admin: '',
                    sort: 0,
                    logo: '' // 菜单标识
                },
                uploadURL: '',
                dataRule: {
                    name: [{
                        required: true,
                        message: '菜单名称不能为空',
                        trigger: 'blur'
                    }],
                    parentName: [{
                        required: true,
                        message: '上级菜单不能为空',
                        trigger: 'change'
                    }],
                    logo: [{
                        required: true,
                        message: '菜单标识不能为空',
                        trigger: 'blur'
                    }],
                    icon: [{
                        required: true,
                        message: '菜单图标不能为空',
                        trigger: 'change'
                    }],
                    url: [{
                        validator: validateUrl,
                        trigger: 'blur',
                        required: true
                    }]
                },
                menuListTreeProps: {
                    label: 'title',
                    children: 'children'
                },
                dialogType: 1,
            };
        },
        methods: {
            handleClose() {
                this.dataForm = {
                    appId: '',
                    type: 1,
                    icon: '', // 菜单图标
                    url: '',
                    name: '',
                    parentid: 0,
                    status: 1,
                    parentName: '',
                    sort: 0,
                    logo: '' // 菜单标识
                };
                this.$refs['dataForm'].resetFields();
            },
            getList() {
                API.menu.getMenuList().then(res => {
                    this.menuList = res.data;
                });
            },
            update(img) {
                this.dataForm.icon = img
            },
            init(item, type, appId) {
                this.visible = true;
                this.getList();
                this.dialogType = type;
                if (item && type == 2) {
                    this.dataForm = {
                        appId: item.appId,
                        type: String(item.type),
                        icon: item.icon, // 菜单图标
                        url: item.url,
                        name: item.name,
                        parentid: item.parentid,
                        status: item.status,
                        parentName: item.parentName,
                        sort: item.sort,
                        id: item.id,
                        logo: item.logo // 菜单标识
                    };
                } else if (item && type == 1) {
                    this.dataForm['appId'] = appId;
                    this.dataForm['parentid'] = item.id;
                } else {
                    this.dataForm['appId'] = appId;
                }
            },
            // 菜单树选中
            menuListTreeCurrentChangeHandle(data, node) {
                console.log(data);
                this.dataForm.parentId = data.id;
                this.dataForm.parentName = data.title;
                this.visible2 = false;
            },
            // 菜单树设置当前选中节点
            menuListTreeSetCurrentNode() {
                this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId);
                this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name'];
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate(valid => {
                    if (valid) {
                        var tick = this.dialogType == 1 ? API.menu.addMenu(this.dataForm) : API.menu.editMenu(
                            this.dataForm);
                        tick.then(data => {
                            if (data.code == 20000000) {
                                this.$message({
                                    message: `${this.dialogType == 2 ? '修改成功' : '新增成功'}`,
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false;
                                        this.$emit('refreshDataList');
                                    }
                                });
                            }
                        });
                    }
                });
            }
        }
    };
</script>